package com.jiashn.dynamic_datasource.dynamic.controller;

import com.jiashn.dynamic_datasource.dynamic.base.AjaxResult;
import com.jiashn.dynamic_datasource.dynamic.domain.SysUser;
import com.jiashn.dynamic_datasource.dynamic.service.ISysUserService;
import com.jiashn.dynamic_datasource.dynamic.util.DataSourceContextHolder;
import com.microsoft.sqlserver.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.jiashn.dynamic_datasource.dynamic.base.AjaxResult.success;

/**
 * 用户信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/usernew")
public class DynamicController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // @Autowired
    // private GenTableColumnMapper genTableColumnMapper;


    @GetMapping("/getData")
    public AjaxResult getMasterData(@RequestParam("datasourceName") String datasourceName) {
        DataSourceContextHolder.setDataSource(datasourceName);
        List<SysUser> list = userService.list(null);
        // DynamicDataSourceContextHolder.clearDataSourceType();
        return success(list);
    }

    @GetMapping("/getDytableData")
    public AjaxResult getDytableData(@RequestParam("datasourceName") String datasourceName, @RequestParam("tableName") String tableName, @RequestParam("flag") boolean flag) {
        DataSourceContextHolder.setDataSource(datasourceName);
        String sql = "";
        if (flag) {
            if (StringUtils.isNumeric(tableName)) {
                sql = "select top 10 *  from [" + tableName + "]";
            } else {
                sql = "select top 10 *  from " + tableName;
            }
        } else {
            sql = "select * from " + tableName + " limit 10";
        }
        String dataSourceType = DataSourceContextHolder.getDataSource();
        System.out.println("数据源为----------------->" + dataSourceType);
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return success(maps);
    }

    // @GetMapping("/getDytableInfo")
    // @ApiOperation("查询数据表结构信息")
    // public AjaxResult getDytableInfo(@RequestParam("datasourceName") String datasourceName, @RequestParam("tableName") String tableName) {
    //     DataSourceContextHolder.setDataSourceType(datasourceName);
    //     List<GenTableColumnVo> genTableColumns;
    //     if (datasourceName.contains("mysql")) {
    //         genTableColumns = genTableColumnMapper.selectDbTableColumnsByNameNew(tableName);
    //     } else {
    //         genTableColumns = genTableColumnMapper.selectSqlServerDbTableByName(tableName);
    //     }
    //     return success(genTableColumns);
    // }
    //
    //
    // @GetMapping("/testConn")
    // public AjaxResult testConn(@RequestParam("datasourceName") String datasourceName) {
    //     Integer integer = null;
    //     try {
    //         DataSourceContextHolder.setDataSourceType(datasourceName);
    //         // 执行一个简单的查询，如果连接成功，则可以返回成功消息
    //         integer = jdbcTemplate.queryForObject("SELECT 1", Integer.class);
    //     } catch (DataAccessException e) {
    //         e.printStackTrace();
    //         return error("连接失败");
    //     }
    //     return integer != null && integer > 0 ? success("连接成功") : error("连接失败");
    // }

}
